package cn.iocoder.yudao.module.pay.controller.admin.collect;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pay.controller.admin.collect.vo.PayCollectOrderPageReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.collect.vo.PayCollectOrderRespVO;
import cn.iocoder.yudao.module.pay.controller.admin.collect.vo.PayCollectOrderSaveReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.paycollectorder.PayCollectOrderDO;
import cn.iocoder.yudao.module.pay.service.paycollectorder.PayCollectOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 收款订单")
@RestController
@RequestMapping("/pay/collect-order")
@Validated
public class PayCollectOrderController {

    @Resource
    private PayCollectOrderService collectOrderService;

    @PutMapping("/check")
    @Operation(summary = "审核收款订单")
    @PreAuthorize("@ss.hasPermission('pay:collect-order:update')")
    public CommonResult<Boolean> checkCollectOrder(@Valid @RequestBody PayCollectOrderSaveReqVO updateReqVO) {
        collectOrderService.checkCollectOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收款订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:collect-order:delete')")
    public CommonResult<Boolean> deleteCollectOrder(@RequestParam("id") Long id) {
        collectOrderService.deleteCollectOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收款订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:collect-order:query')")
    public CommonResult<PayCollectOrderRespVO> getCollectOrder(@RequestParam("id") Long id) {
        PayCollectOrderDO collectOrder = collectOrderService.getCollectOrder(id);
        return success(BeanUtils.toBean(collectOrder, PayCollectOrderRespVO.class));
    }

    @GetMapping("/get-by-orderNo")
    @Operation(summary = "获得收款订单")
    public CommonResult<PayCollectOrderRespVO> getByOrderNo(@RequestParam("orderNo") String orderNo) {
        PayCollectOrderDO collectOrder = collectOrderService.getCollectOrderByOrderNo(orderNo);
        return success(BeanUtils.toBean(collectOrder, PayCollectOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收款订单分页")
    @PreAuthorize("@ss.hasPermission('pay:collect-order:query')")
    public CommonResult<PageResult<PayCollectOrderRespVO>> getCollectOrderPage(@Valid PayCollectOrderPageReqVO pageReqVO) {
        PageResult<PayCollectOrderDO> pageResult = collectOrderService.getCollectOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PayCollectOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收款订单 Excel")
    @PreAuthorize("@ss.hasPermission('pay:collect-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollectOrderExcel(@Valid PayCollectOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayCollectOrderDO> list = collectOrderService.getCollectOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收款订单.xls", "数据", PayCollectOrderRespVO.class,
                        BeanUtils.toBean(list, PayCollectOrderRespVO.class));
    }

}